﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Business;
using Geekees.Common.Controls;

public partial class Controls_BackEnd_NewsCategoryOrder : UserControl
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        InitializeComponent();
        GenerateTree();
    }



    /// <summary>
    /// 
    /// </summary>
    private void InitializeComponent()
    {
        astvMyTree.ContextMenu.MenuItems.Add(new ASContextMenuItem("Custom Menu 1", "alert('current value:' + " + astvMyTree.ContextMenuClientID + ".getSelectedItem().parentNode.getAttribute('treeNodeValue')" + ");return false;", "otherevent"));
        astvMyTree.ContextMenu.MenuItems.Add(new ASContextMenuItem("Custom Menu 2", "alert('current text:' + " + astvMyTree.ContextMenuClientID + ".getSelectedItem().innerHTML" + ");return false;", "otherevent"));
        astvMyTree.EnableContextMenuAdd = false;
        astvMyTree.EnableContextMenuDelete = false;
        astvMyTree.EnableContextMenuEdit = false;
    }



    /// <summary>
    /// 
    /// </summary>
    private void GenerateTree()
    {

        astvMyTree.EnableAjaxOnEditDelete = false;
        var newsCategorieses = new NewsCategories().NewsCategories_GetByCondition(string.Empty);
        Session[SessionManager.SessionList] = newsCategorieses;
        var father = newsCategorieses.FindAll(f => f.FatherID == 0);
        var chirld = newsCategorieses.FindAll(c => father.TrueForAll(f => f
                                                                          .
                                                                          FatherID !=
                                                                      c
                                                                          .
                                                                          FatherID));
        father.AddRange(chirld);
        father.Sort((f1, f2) => f1.Order.CompareTo(f2.Order));
        var dataTable = new GenericToDataTable().ConvertTo(father);
        var descripter = new ASTreeViewDataTableColumnDescriptor("Name"
                                                                  , "CateId"
                                                                  , "FatherID");
        astvMyTree.DataSourceDescriptor = descripter;
        astvMyTree.DataSource = dataTable;
        astvMyTree.DataBind();
    }





    /// <summary>
    /// 
    /// </summary>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnBack":
                Config.Back();
                break;
            case "btnSave":
                Int16 index = 0;
                var newsCategorieses = (List<NewsCategories>)Session[SessionManager.SessionList];
                foreach (var parentNode in astvMyTree.RootNode.ChildNodes)
                {
                    var node = parentNode;
                    var functions = newsCategorieses.Find(f => f.CateID == int.Parse(node.NodeValue));
                    functions.Order = index;
                    functions.Save();
                    Int16 subIndex = 0;
                    foreach (var subfun in
                        parentNode.ChildNodes.Select(subnode => newsCategorieses.Find(c => c.CateID == int.Parse(subnode.NodeValue))))
                    {
                        subfun.FatherID = functions.CateID;
                        subfun.Order = subIndex;
                        subfun.Save();
                        subIndex++;
                    }
                    index++;
                }
                break;
        }
    }
}
